<?php
// +----------------------------------------------------------------------
// | quanli team
// +----------------------------------------------------------------------
// | Copyright (c) 2013-2020 http://www.fistforward.com/ All rights reserved.
// +----------------------------------------------------------------------
// | create by allen.qiang
// +----------------------------------------------------------------------


/**
 * 中奖记录操作功能
 * @package  htdocs
 * @subpackage  quanliadmin
 * @create by allen.qiang
 */

quanli_require('module/base/promotionHandler.class.php');

class pa_winRecord {

    public function execute() {

        if (isset($_REQUEST['operFlg']) && is_numeric($_REQUEST['operFlg'])) {

            switch ($_REQUEST['operFlg']) {

                case 1: //中奖记录列表信息
                    $this-> winRecordList();
                    break;

                case 2: //中奖记录导出
                    $this-> winRecordExport();
                    break;

                default:
                    $this->toError('参数错误');
                    break;
            }

        } else {

            $this->toError('参数错误');

        }




    }

    private function toError( $msg ) {

        global $G_PGLOB;

        $G_PGLOB->display_error_page( $msg );

    }

    /**
     * 优惠券列表信息
     */
    private function winRecordList() {

        global $G_PGLOB;

        $promotionID = $_REQUEST['promotionID'];

        //得到促销活动类型
        $promotionHandler = new promotionHandler($G_PGLOB->DBCA);

        $arg_array['promotionID'] = $promotionID;

        $promotion_weixin = $promotionHandler->searchWxActivity($arg_array);

        $G_PGLOB->smarty->assign('promotionTypeName',$promotion_weixin[0]['promotionTypeName']);

        $G_PGLOB->smarty->assign('promotionID',$promotionID);

        $G_PGLOB->smarty->display('promotion/winRecordList.tpl');

    }

    //导出中奖纪录
    private function winRecordExport(){
        global $G_PGLOB;

        //检查phpexcel类
        quanli_include('framework/phpExecl/PHPExcel.php');
        quanli_include('framework/phpExecl/PHPExcel/Writer/Excel2007.php');

        $arg_array = $_REQUEST;

        $arg_array['sortname'] = 'createTime';
        $arg_array['sortorder'] = 'desc';

        $promotionHandler = new promotionHandler($G_PGLOB->DBCA);

        //查询中奖纪录
        $result_data = $promotionHandler->searchWinLogInfo($arg_array);


        //实例excel类
        $objPHPExcel = new PHPExcel();

        /**
         * 设置excel的属性
         */
        //创建人
        $objPHPExcel->getProperties()->setCreator("$G_PGLOB->currentUserName");
        //最后修改人
        $objPHPExcel->getProperties()->setLastModifiedBy("$G_PGLOB->currentUserName");
        //标题
        $objPHPExcel->getProperties()->setTitle("中奖纪录明细列表");
        //题目
        $objPHPExcel->getProperties()->setSubject("中奖纪录明细列表");
        //描述
        $objPHPExcel->getProperties()->setDescription("中奖纪录明细列表");
        /**
         * 设置excel的属性
         */

        /**
         * sheet
         */
        //设置当前的sheet
        $objPHPExcel->setActiveSheetIndex(0);
        //设置sheet的name
        $objPHPExcel->getActiveSheet()->setTitle('中奖纪录明细列表');
        /**
         * sheet
         */


        /**
         * 设置标题
         */
        $objPHPExcel->getActiveSheet()->setCellValue('A1', '会员号');
        $objPHPExcel->getActiveSheet()->setCellValue('B1', '奖项名称');
        $objPHPExcel->getActiveSheet()->setCellValue('C1', '奖品');
        $objPHPExcel->getActiveSheet()->setCellValue('D1', '是否已领取');
        /**
         * 设置标题
         */

        /**
         * 添加数据
         */
        if ($result_data) {

            $i = 2;

            foreach($result_data as &$val) {

                $objPHPExcel->getActiveSheet()->setCellValue('A' . $i, $val['pcustID'] . "\t");
                $objPHPExcel->getActiveSheet()->setCellValue('B' . $i, $val['awardsName']);
                $objPHPExcel->getActiveSheet()->setCellValue('C' . $i, $val['prize']);
                $objPHPExcel->getActiveSheet()->setCellValue('D' . $i, $val['isReceiveName']);
                $i++;

            }

        }
        /**
         * 添加数据
         */

        $objPHPExcel->getActiveSheet()->getColumnDimension('A')->setWidth(15);
        $objPHPExcel->getActiveSheet()->getColumnDimension('B')->setWidth(15);
        $objPHPExcel->getActiveSheet()->getColumnDimension('C')->setWidth(25);
        $objPHPExcel->getActiveSheet()->getColumnDimension('D')->setWidth(15);

        //文件名称
        $fileName = '中奖纪录_' . date('YmdHis') . '.xlsx';

        $objWriter = new PHPExcel_Writer_Excel2007($objPHPExcel);
        header("Content-Type: application/force-download");
        header("Content-Disposition:inline;filename=$fileName");
        header("Content-Transfer-Encoding: binary");
        header("Cache-Control: must-revalidate, post-check=0, pre-check=0");
        header("Pragma: no-cache");
        $objWriter->save('php://output');
    }

}